package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.CreditPayService;
import cc.rengu.igas.channel.upac.facade.request.CreditTransStateQueryRequest;
import cc.rengu.igas.channel.upac.facade.response.CreditTransStateQueryRespone;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 贷记交易状态查询服务 on 2019/10/8.
 */
public class CreditTransStateQueryService extends CreditPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CreditTransStateQueryRequest transStateQueryRequest = new CreditTransStateQueryRequest();
        ConvertUtil.convertOutput(transStateQueryRequest);
        return transStateQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CreditTransStateQueryRespone creditTransStateQueryRespone = new CreditTransStateQueryRespone();
        CreditTransStateQueryRequest transStateQueryRequest = (CreditTransStateQueryRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(transStateQueryRequest);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.PAYMENT.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, transStateQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            creditTransStateQueryRespone.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            creditTransStateQueryRespone.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(creditTransStateQueryRespone);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(creditTransStateQueryRespone);
        creditTransStateQueryRespone.setOrigRespCode(convertRespCode(creditTransStateQueryRespone.getOrigRespCode()));
        if (UpacParamConstant.SUCCESS_CODE.equals(creditTransStateQueryRespone.getRespCode()) ||
                UpacParamConstant.DEFECT_SUCCESS_CODE.equals(creditTransStateQueryRespone.getRespCode())) {
            creditTransStateQueryRespone.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            creditTransStateQueryRespone.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        } else {
            creditTransStateQueryRespone.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            creditTransStateQueryRespone.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
        }
        if (creditTransStateQueryRespone.getPayAcctNo() != null) {
            if ("Y".equals(acctEncFlag)) {
                rglog.error("配置账户加密，解密账号");
                creditTransStateQueryRespone.setPayAcctNo(descryptData(creditTransStateQueryRespone.getPayAcctNo()));
            }
        }
        chnlBizResponse.setResult(creditTransStateQueryRespone);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CreditTransStateQueryRespone creditTransStateQueryRespone = (CreditTransStateQueryRespone) response.getResult();
        ConvertUtil.convertInput(creditTransStateQueryRespone);
    }


}
